package com.ele.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ele.pojo.Food;
import com.ele.pojo.Shop;
import com.ele.utils.JDBCUtils;


public class FoodDao {
	/**
	 * 	添加菜品信息到数据库
	 * @param food	菜品信息
	 * @return	返回受影响行数作为判断是否添加成功
	 * @throws SQLException
	 */
	public Integer addFood(Food food) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "insert into food values(null,?,?,?,?,?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setInt(1, food.getShop_id());
		ptmt.setString(2, food.getFoodName());
		ptmt.setInt(3, food.getFoodPrice());
		ptmt.setString(4, food.getFoodDesc());
		ptmt.setInt(5,food.getFoodStatus());
		
		return ptmt.executeUpdate();
	}
	/**
	 * 	查询某商铺下所有菜品信息
	 * @param shop	该商店
	 * @return	返回存储菜品信息的ArrayList数组
	 * @throws SQLException
	 */
	public List<Food> selectFood(Shop shop) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from food";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ResultSet rs  = ptmt.executeQuery();
		List<Food> list = new ArrayList<>();
		while (rs.next()) {
			Food food = new Food();
			food.setId(rs.getInt("id"));
			food.setShop_id(rs.getInt("shopid"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getInt("foodPrice"));
			food.setFoodStatus(rs.getInt("foodStatus"));
			food.setFoodDesc(rs.getString("foodDesc"));
			list.add(food);
		}
		JDBCUtils.close(conn, ptmt,rs);
		return list;
		
	}
	/**
	 * 	模糊查询菜品信息
	 * @param shop
	 * @param name
	 * @return
	 * @throws SQLException 
	 */
	public List<Food> selectFood(Shop shop, String name) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from food where foodName like ?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, "%"+name+"%");
		ResultSet rs = ptmt.executeQuery();
		List<Food> list = new ArrayList<>();
		while (rs.next()) {
			Food food = new Food();
			food.setId(rs.getInt("id"));
			food.setShop_id(rs.getInt("shopid"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getInt("foodPrice"));
			food.setFoodStatus(rs.getInt("foodStatus"));
			food.setFoodDesc(rs.getString("foodDesc"));
			list.add(food);
		}
		JDBCUtils.close(conn, ptmt,rs);
		return list;
	}
	public Food selectFood(Shop shop, Integer id) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from food where id = ? and shopId = ?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setInt(1, id);
		ptmt.setInt(2, shop.getId());
		ResultSet rs = ptmt.executeQuery();
		if (rs.next()) {
			Food food = new Food();
			food.setId(rs.getInt("id"));
			food.setShop_id(rs.getInt("shopid"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getInt("foodPrice"));
			food.setFoodStatus(rs.getInt("foodStatus"));
			food.setFoodDesc(rs.getString("foodDesc"));
			JDBCUtils.close(conn, ptmt, rs);
			return food;
		}
		return null;
	}

}
